package com.rlz.base.util.download;


import org.jetbrains.annotations.NotNull;

import java.io.Serializable;
import java.text.DecimalFormat;

/**
 * Created by RLZ
 * on 2021/4/22
 */
public class FileInfo implements Serializable {
    private String id;                  //文件的唯一标识符 (url+文件存储路径)
    private String downloadUrl;         //下载的url
    private String filePath;            //文件存放的路径位置
    private long size;                  //文件的总尺寸
    private long downloadLocation;      // 下载的位置(就是当前已经下载过的size，也是断点的位置)

    private int downloadStatus = DownloadStatus.PAUSE;   //下载的状态信息
    private DecimalFormat mDecimalFormat;


    public FileInfo() {
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getDownloadUrl() {
        return downloadUrl;
    }

    public void setDownloadUrl(String downloadUrl) {
        this.downloadUrl = downloadUrl;
    }

    public String getFilePath() {
        return filePath;
    }

    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }

    public long getSize() {
        return size;
    }

    public void setSize(long size) {
        this.size = size;
    }

    public long getDownloadLocation() {
        return downloadLocation;
    }

    public void setDownloadLocation(long downloadLocation) {
        this.downloadLocation = downloadLocation;
    }

    public int getDownloadStatus() {
        return downloadStatus;
    }

    public void setDownloadStatus(int downloadStatus) {
        this.downloadStatus = downloadStatus;
    }


    private int getTotalSize() {
        float totalSize = getSize() / 1024.0f / 1024;
        return (int) totalSize;
    }

    public int getCalculateProgress() {
        float pro = (float) (getDownloadLocation() * 1.0 / getSize());
        return Math.round(pro * 100);
    }

    private int getDownSize() {
        return Math.round(getDownloadLocation() / 1024.0f / 1024);
    }

    private String parse(float flo) {
        if (mDecimalFormat == null)
            mDecimalFormat = new DecimalFormat("0");
        return mDecimalFormat.format(flo);
    }


    public String getCalculateTotal() {
        return parse(getTotalSize()) + "MB\n";
    }


    public String getCalculateDown() {
        return parse(getDownSize()) + "MB/";
    }

    @NotNull
    @Override
    public String toString() {
        return "FileInfo{" +
                "id='" + id + '\'' +
                ", downloadUrl='" + downloadUrl + '\'' +
                ", filePath='" + filePath + '\'' +
                ", size=" + size +
                ", downloadLocation=" + downloadLocation +
                ", downloadStatus=" + getStatusDesc(downloadStatus) +
                '}';
    }


    public String getStatusDesc(int status) {
        switch (status) {
            case DownloadStatus.WAIT:
                return " wait ";
            case DownloadStatus.PREPARE:
                return " prepare ";
            case DownloadStatus.LOADING:
                return " loading ";
            case DownloadStatus.PAUSE:
                return " pause ";
            case DownloadStatus.COMPLETE:
                return " complete ";
            case DownloadStatus.FAIL:
                return " fail ";
            default:
                return "  错误的未知状态 ";
        }
    }
}
